<template>
	<view class="next">
		<view class="next-top">
			<text>设置密码</text>
			<text>全新的体验，开启世界大门！</text>
		</view>
		<view class="next-input">
			<view class="next-phone input">
				<text class="iconfont icon-pass"></text>
				<input type="password" v-model="password" placeholder="请输入密码" />
			</view>
			<view class="next-pass input">
				<text class="iconfont icon-pass"></text>
				<input type="password" v-model="password2" placeholder="请再次输入密码" />
			</view>
			<view class="next-pass input">
				<text class="iconfont icon-y-code"></text>
				<input type="text" :disabled="isDisabled" v-model="invitation_code" placeholder="请输入邀请码(可选)" />
			</view>
			<view class="btns">
				<view class="btn">
					<button @click="submit()" :loading="loading" :disabled="loading" type="default">完成</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		reg
	} from '@/api/entrance.js'
	import {
		systemInfo,
	} from "@/utils/util.js"
	// import {
	// 	clearData,
	// } from "@/mining/BrowserMining.js";
	// import {
	// 	appClearData,
	// } from "@/mining/AppMining.js";
	import {mapMutations} from 'vuex';
	export default{
		name:"next",
		data(){
			return{
				password:"",
				password2:"",
				loading:false,
				invitation_code:"",
				isDisabled:false
			}
		},
		mounted() {
			let share_code = uni.getStorageSync("share_code");
			if(share_code){
				this.invitation_code = share_code;
				this.isDisabled = true;
			}
		},
		methods:{
			...mapMutations(['setUserInfo_mutation', 'setIsOpenMini_mutation']),
			async submit(){
				if(!this.password){
					this.$msg("密码不能为空！")
					return;
				}
				if(!this.password2){
					this.$msg("重复密码不能为空！")
					return;
				}
				if(this.password !== this.password2){
					this.$msg("两次密码不一致！")
					return;
				}
				this.loading = true;
				let res = await reg({
					password:this.password,
					deviceId:uni.getSystemInfoSync().deviceId,
					invitation_code:this.invitation_code
				});
				if(res.code === 1){
					this.loading = false;
					this.setUserInfo_mutation(res.data);
					uni.setStorageSync("oauth_token",res.data.token);
					//无论何种情况 注册成功 都清除数据
					// if (systemInfo() === "browser") {
					// 	await clearData()
					// } else {
					// 	await appClearData()
					// }
					this.setIsOpenMini_mutation(false)
					uni.switchTab({
						url: "/pages/users/index"
					})
				}else{
					this.loading = false;
					this.$msg(res.msg)
				}
			}
		}
	}
</script>

<style scoped lang="less" src="./next.less">
</style>
